<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\controller\Admin;
use think\Db;

class Nav extends Admin {

    /**
     * @title 导航框架
     */
    public function iframe() {

        // 加载内容
        $this->assign('iframe_src', url('admin/nav/index'));
        // 加载分类
        $this->assign('nodes', model('nav_cat')->iframe_category_json());
        return $this->fetch('base:iframe');
    }

    /**
     * @title 导航链接的选取
     */
    public function href_select() {

        $type = input('get.type', 'article_cat');
        // 分类
        $lists = [];
        if ($type == 'article_cat') {

            $lists = model('article_cat')->model_where()->select();

            // 转成树形
            $lists = collection($lists)->toArray();
            $tree = new \utils\Tools\Tree($lists);
            $lists = $tree->getArray(0);

            foreach ($lists as $key => $value) {
                $lists[$key]['href'] = 'index/index/lists_article?id=' . $value['id'];
            }
        } elseif ($type == 'article') {

            $lists = model('article')->model_where()->paginate(5, false, ['query' => request()->get()]);
            foreach ($lists as $key => $value) {
                $lists[$key]['href'] = 'index/index/views_article?id=' . $value['id'];
            }
        } else if ($type == 'goods_cat') {

            $lists = model('goods_cat')->model_where()->select();

            // 转成树形
            $lists = collection($lists)->toArray();
            $tree = new \utils\Tools\Tree($lists);
            $lists = $tree->getArray(0);

            foreach ($lists as $key => $value) {
                $lists[$key]['href'] = 'index/index/lists_goods?id=' . $value['id'];
            }
        } else if ($type == 'goods') {

            $lists = model('goods')->model_where()->paginate(5, false, ['query' => request()->get()]);
            foreach ($lists as $key => $value) {
                $lists[$key]['href'] = 'index/index/views_goods?id=' . $value['id'];
            }
        }

        $this->assign('lists', $lists);

        if ($type == 'article' || $type == 'goods')
            $this->assign('pages', $lists->render());
        else
            $this->assign('pages', '');

        return view();
    }

    /**
     * @title 导航列表
     */
    public function index() {

        $cid = input('get.category', null);



        session('redirect', url('index') . '?' . http_build_query(request()->get()));





        $lists = model('nav')->model_where()->select();


        // 如果这里不加载额外的数据，在模板输出的时候加载不出来
        foreach ($lists as $key => $value) {
            $lists[$key]['category_text'] = $value->append(['cid_text'])->toArray();
        }

        // 转成树形
        $lists = collection($lists)->toArray();
        $tree = new \utils\Tools\Tree($lists);
        $this->assign('lists', $tree->getArray(0));

        // 分类转移 
        $this->assign('category_select', model('nav_cat')->column('id,title'));
        builder('list')
                ->addItem('id', '#')
                ->addItem('cid_text', '分类')
                ->addSortItem('listorder', '排序', 'nav')
                ->addItem('title', '标题')
                ->addItem('image', '图片', 'image')
                ->addItem('href', '链接')
                ->addAction('编辑', 'edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal')
                ->addAction('删除', 'delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();

        return $this->fetch();
        // return view();
    }

    /**
     * @title 导航添加
     */
    public function add() {

        if (request()->isPost()) {

            $post = request()->post();

            $insert_id = db('nav')->insertGetId($post);

            if (is_numeric($insert_id)) {

                db('nav')->where('id', $insert_id)->setField('listorder', $insert_id);

                $this->success('', session('redirect'));
            } else {
                $this->error('添加失败');
            }
        } else {


            $cid = input('get.cid', 0);


            builder('form')
                    ->addItem('pid', 'select', '上一级', model('nav')->lists_select_tree('cid=' . $cid))
                    ->addItem('cid', 'select', '分类', model('nav_cat')->column('id,title'))
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('image', 'image', '图片', '', '', '', '200x100')
                    ->addItem('href', 'input_select', '链接', 'href_select')
                    ->addItem('target', 'input', '打开方式')
                    ->addItem('icon', 'input', '图标')
                    ->addItem('description', 'textarea', '描述')
                    ->build(['cid' => $cid]);
            return view();
        }
    }

    /**
     * @title 导航编辑
     */
    public function edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {

            $post = request()->post();



            if (db('nav')->where('id', $id)->update($post) !== FALSE) {




                $this->success('', session('redirect'));
            } else {
                $this->error('添加失败');
            }
        } else {


            $one = db('nav')->where('id', $id)->find();

            builder('form')
                    ->addItem('pid', 'select', '上一级', model('nav')->lists_select_tree(['id' => ['<>' , $id], 'cid' => ['=', $one['cid']]]))
                    ->addItem('cid', 'select', '分类', model('nav_cat')->column('id,title'))
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('image', 'image', '图片', '', '', '', '200x100')
                    ->addItem('href', 'input_select', '链接', 'href_select')
                    ->addItem('target', 'input', '打开方式')
                    ->addItem('icon', 'input', '图标')
                    ->addItem('description', 'textarea', '描述')
                    ->build($one);
            return view();
        }
    }

    /**
     * @title 导航删除
     */
    public function delete($id) {

        empty($id) && $this->error('参数不能为空');
        if (db('nav')->where('id', $id)->delete() !== FALSE) {
            $this->success('');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 批量删除
     */
    public function deletes($ids) {
        empty($ids) && $this->error('参数不能为空');
        $affect_row = db('nav')->where('id', 'in', $ids)->delete();
        if ($affect_row) {
            $this->success('');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @title 导航分类转移 
     */
    public function set_category() {
        $value = input('get.value', 0);
        $ids = input('post.ids');
        if (!empty($ids)) {
            $affect_rows = db('nav')->where('id', 'in', $ids)->setField('cid', $value);
            $affect_rows ? $this->success('完成') : $this->success('没有更新任何数据');
        } else {
            $this->error('请选择要操作的数据');
        }
    }

    /**
     * @title 导航分类
     * @return type
     */
    public function category() {


        session('redirect', url('category') . '?' . http_build_query(request()->get()));

        $lists = model('nav_cat')->select();

        $this->assign('lists', $lists);

        builder('list')
                ->addItem('id', '#')
                ->addSortItem('listorder', '排序', 'nav_cat')
                ->addItem('title', '标题')
                ->addItem('alias', '别名')
                ->addAction('编辑', 'category_edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal openbox')
                ->addAction('删除', 'category_delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();
        return view();
    }

    /**
     * @导航分类添加
     */
    public function category_add() {

        if (request()->isPost()) {

            $post = request()->post();

            $insert_id = db('nav_cat')->insertGetId($post);

            if (is_numeric($insert_id)) {

                // 有排充的话就更新一下
                db('nav_cat')->where('id', $insert_id)->setField('listorder', $insert_id);


                $this->success('', session('redirect'));
            } else {
                $this->error('添加失败');
            }
        } else {
            builder('form')
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('alias', 'input', '别名<font color="red">*</font>', '', 'lay-verify="required"')
                    ->build();
            return view();
        }
    }

    /**
     * @title 导航分类编辑
     */
    public function category_edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {
            $post = request()->post();

            if (db('nav_cat')->where('id', $id)->update($post) !== FALSE) {
                $this->success('', session('redirect'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $one = db('nav_cat')->where('id', $id)->find();
            builder('form')
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('alias', 'input', '别名<font color="red">*</font>', '', 'lay-verify="required"')
                    ->build($one);
            return view();
        }
    }

    /**
     * @title 导航分类删除
     */
    public function category_delete($id) {

        empty($id) && $this->error('参数不能为空');

        if (db('nav_cat')->where('id', $id)->delete() !== FALSE) {
            $this->success('', session('redirect'));
        } else {
            $this->error('删除失败');
        }
    }

}
